package cj.web.yc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.paoding.rose.web.Invocation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cj.web.yc.constants.VideoTypeEnum;
import cj.web.yc.dao.VideoDAO;
import cj.web.yc.domain.Video;

@Service
public class VideoService {
	private final static String VIDEO_URL="VIDEO_URL";
	
	@Autowired
    private VideoDAO videoDAO;

	/**
	 * 查询
	 * @param video
	 * @return videolis;size
	 */
	public Map query(Video video,Invocation inv){
		//取加载的值
		ServletContext context = inv.getServletContext();  
        HashMap dictionaryInfoMap = (HashMap)context.getAttribute("dictionaryInfoMap");
		Map configRep = (Map) dictionaryInfoMap.get("configRep");//配置
		String headUrl = configRep.get(VIDEO_URL).toString();
		
		Map rep = new HashMap();
		List<Video> videolis = videoDAO.query(video);
		for(Video obj:videolis){
			obj.setTypeName(VideoTypeEnum.getName(obj.getType()));
			if(VideoTypeEnum.SP.getValue()==obj.getType()){
				obj.setUrl(headUrl+obj.getUrl());
			}
		}
		int size = videoDAO.querySize(video); 
		rep.put("videolis", videolis);
		rep.put("size", size);
		return rep;
    }
	
	public Video queryById(int id){
		Video obj = videoDAO.queryById(id);
		return obj;
    }
	
	public void create(Video video){
		videoDAO.create(video);
	}
	
	public void update(Video video){
		videoDAO.update(video);
	}
	
	public void deleteById(int id){
		videoDAO.deleteById(id);
	}
}
